package com.zhouyilin.bigevent2.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhouyilin.bigevent2.mapper.ArticleMapper;
import com.zhouyilin.bigevent2.pojo.Article;
import com.zhouyilin.bigevent2.pojo.PageBean;
import com.zhouyilin.bigevent2.service.ArticleService;
import com.zhouyilin.bigevent2.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void add(Article article) {
        Map<String ,Object> map =ThreadLocalUtil.get();
        Integer createId = (Integer) map.get("id");
        article.setCreateUser(createId);
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.add(article);
    }

    @Override
    public PageBean list(Integer pageNum, Integer pageSize, Integer categoryID, String state) {
//        1、创建pagebean对象
        PageBean<Article> pageBean = new PageBean();

//        2、开启分页查询  Pagehelper
        PageHelper.startPage(pageNum,pageSize);
//        3、调用mapper
        Map<String ,Object> map =ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        List<Article> articleList = articleMapper.list(categoryID, state,userId);
        Page<Article> p = (Page<Article>) articleList;
        pageBean.setTotal(p.getTotal());
        pageBean.setItems(p.getResult());
        return pageBean;
    }

    @Override
    public Article getDetailById(Integer id) {
        Article article = articleMapper.getDetailById(id);
        return article;
    }

    @Override
    public void update(Article article) {
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.update(article);
    }

    @Override
    public void deleteById(Integer id) {
        articleMapper.deleteById(id);
    }
}
